package com.mew.slots.core.game.mymg.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.exception.ResponseCode;
import com.mew.common.util.GameUtil;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.utils.SlotsGameUtil;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.dwsl.bo.DwslInRoomList;
import com.mew.slots.core.game.dwsl.message.DwslBonusPlayResponseMessage;
import com.mew.slots.core.game.mymg.message.MymgBonusPlayResponseMessage;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MymgBonusService {
    private static final int[] ITEM={10,4,2,5,4,2,30,4,2,20,4,2};//选项
    private static final int[] ITEM_WEIGHTS={30,10,1,10,10,1,10,10,1,20,10,1};//选项权重
    public SlotsBonusPlayResponseMessage play(StrategyGameVO player){
        SlotsBonusPlayResponseMessage message=new SlotsBonusPlayResponseMessage();
        WinFree winFree=player.getWinFree();
        if(winFree.getBounsNumber() < 1){
            player.setStat(ResponseCode.BounsCanNotPlay.code());
            return message;
        }
        player.setWinType(SlotsWinType.Special.code());
        player.setInBetNumber(winFree.getBet());

        MymgBonusPlayResponseMessage bonusPlayResponseMessage = playGame(player);
        winFree.setBounsNumber(winFree.getBounsNumber()-1);
        if(winFree.getBounsNumber() == 0){
            bonusPlayResponseMessage.setIsGameOver(1);
            bonusPlayResponseMessage.setPayment(bonusPlayResponseMessage.getAllRate() * player.getInBetNumber() * player.getLineNumber());
            player.setPaymentNumber(bonusPlayResponseMessage.getAllRate() * player.getInBetNumber() * player.getLineNumber());
            player.setBetTimes(bonusPlayResponseMessage.getAllRate());
            winFree.setBounsRate(0);
        }
        bonusPlayResponseMessage.setBonusNum(winFree.getBounsNumber());
        return bonusPlayResponseMessage;
    }

    /**
     * @param player
     * @return
     */
    private MymgBonusPlayResponseMessage playGame(StrategyGameVO player){
        WinFree winFree = player.getWinFree();
        MymgBonusPlayResponseMessage message=new MymgBonusPlayResponseMessage();
        int randomIndex= GameUtil.weights(ITEM_WEIGHTS);
        int randomResult = ITEM[randomIndex];
        winFree.setBounsRate(winFree.getBounsRate()+randomResult);
        message.setIndex(randomIndex);
        message.setAllRate(winFree.getBounsRate());
        player.setIconResult(String.valueOf(randomResult));
        return message;
    }

    public static void main(String[] args) {
        MymgBonusService mb = new MymgBonusService();
        StrategyGameVO vo = SlotsGameUtil.buildTestStrategyGameVO();
        vo.setLineNumber(50);
        WinFree winFree = vo.getWinFree();
        int rate = 0;
        for (int i = 0; i < 1000000; i++) {
            winFree.setBounsNumber(1);
            winFree.setBounsRate(0);
            MymgBonusPlayResponseMessage res = mb.playGame(vo);
            rate += winFree.getBounsRate();
        }
        System.out.println(rate/1000000*3);
    }
}
